$(window).scroll(function () {
    scrollFunction();
})


function scrollFunction() {
    var wintop = document.body.scrollTop + document.documentElement.scrollTop;

    if (wintop > 760) {
        $(".elevator").css('display', 'block');
        if (wintop > 760 && wintop < 1240) {
            $(".ele_item1").css('color', '#00c5c3');
            $(".ele_item2").css('color', 'black');
            $(".ele_item3").css('color', 'black');
            $(".ele_item4").css('color', 'black');
        } else if (wintop >= 1240 && wintop < 1840) {
            $(".ele_item1").css('color', 'black');
            $(".ele_item2").css('color', '#00c5c3');
            $(".ele_item3").css('color', 'black');
            $(".ele_item4").css('color', 'black');
        } else if (wintop >= 1840 && wintop < 2440) {
            $(".ele_item1").css('color', 'black');
            $(".ele_item2").css('color', 'black');
            $(".ele_item3").css('color', '#00c5c3');
            $(".ele_item4").css('color', 'black');
        } else if (wintop >= 2440 && wintop < 3410) {
            $(".ele_item1").css('color', 'black');
            $(".ele_item2").css('color', 'black');
            $(".ele_item3").css('color', 'black');
            $(".ele_item4").css('color', '#00c5c3');
        }
    } else {
        $(".elevator").css('display', 'none');
    }
}

$(".ele_item1").click(function () {
    console.log("点击");
    document.body.scrollTop = 761;
});
$(".ele_item2").click(function () {
    document.body.scrollTop = 1241;
});
$(".ele_item3").click(function () {
    document.body.scrollTop = 1971;
});
$(".ele_item4").click(function () {
    document.body.scrollTop = 22545;
});
$(".ele_item5").click(function () {
    document.body.scrollTop = 0;
});


/*======================定位================================*/
getiCity();


function getiCity() {
    var site = $(".site");
    site.html("");
    var key = "c98b1a453d519145809e1eb692cc7376";
    $.ajax({
        type: "get",
        // async:false,
        url: "https://restapi.amap.com/v3/ip?key=" + key + "&output=JSON",
        success: function (jsonResult) {
            var length = jsonResult.city.length;
            var city = jsonResult.city.substring(0, length - 1);
            site.html(city);
            console.log("当前城市为：" + site.html());

            getHotSpot();
            getRimSpot();
            getLongSpot();
            getSecKillList();
        }
    })

}

/*===============首页数据渲染=======================*/

/*
* 获得热门景点
* 并渲染在首页
* */
function getHotSpot() {
    var box = $(".hot-box-ul");
    box.html("");
    $.ajax({
        type: "get",
        url: "/product/gethotproduct",
        datatype: "json",
        success: function (jsonResult) {
            console.log(jsonResult);
            var hotProductList = jsonResult.data;
            for (var i = 0; i < hotProductList.length && i < 8; i++) {
                var previewImg = hotProductList[i].previewImg;
                var productName = hotProductList[i].productName;
                var proId = hotProductList[i].id;
                var productDescription = hotProductList[i].productDescription;
                var personPrice = hotProductList[i].personPrice;
                var path = location.pathname;
                var webPath = path.substring(0, path.substr(1).indexOf('/') + 1);
                var hotProduct = " <input type=\"hidden\" class=\"proId\" value=\"" + proId + "\"> \n" +
                    " <li class=\"hot-li\"><a href=\"/product/getOne/" + proId + "\">\n" +
                    "                <div class=\"hot-li-img\"><img src=\"" + webPath + previewImg + "\"></div>\n" +
                    "                <div class=\"hot-li-message\">\n" +
                    "                    <span class=\"hot-item-title\">" + productName + "</span>\n" +
                    "                    <span class=\"hot-item-detail\">" + productDescription + "</span>\n" +
                    "                    <span class=\"money-sy\">￥</span><span class=\"hot-item-money\">" + personPrice + "</span>\n" +
                    "                </div>\n" +
                    "            </a> </li>";
                box.append(hotProduct);
            }
        }
    })
}

//渲染周边游
function getRimSpot() {
    var site = $(".site").html();
    console.log("当前地点：" + site);
    var box = $(".rim-box-ul");
    box.html("");
    $.ajax({
        type: "get",
        url: "/product/getrimproduct?site=" + site,
        datatype: "json",
        success: function (jsonResult) {

            console.log(jsonResult);
            var rimProductList = jsonResult.data;
            if (rimProductList != "获取数据失败") {
                for (var i = 0; i < rimProductList.length && i < 8; i++) {
                    var previewImg = rimProductList[i].previewImg;
                    var productName = rimProductList[i].productName;
                    var proId = rimProductList[i].id;
                    var productDescription = rimProductList[i].productDescription;
                    var personPrice = rimProductList[i].personPrice;
                    var path = location.pathname;
                    var webPath = path.substring(0, path.substr(1).indexOf('/') + 1);
                    var rimProduct = " <input type=\"hidden\" class=\"proId\" value=\"" + proId + "\"> \n" +
                        " <li class=\"rim-li\"><a href=\"/product/getOne/" + proId + "\">\n" +
                        "                <div class=\"rim-li-img\"><img src=\"" + webPath + previewImg + "\"></div>\n" +
                        "                <div class=\"rim-li-message\">\n" +
                        "                    <span class=\"rim-item-title\">" + productName + "</span>\n" +
                        "                    <span class=\"rim-item-detail\">" + productDescription + "</span>\n" +
                        "                    <span class=\"money-sy\">￥</span><span class=\"rim-item-money\">" + personPrice + "</span>\n" +
                        "                </div>\n" +
                        "            </a> </li>";
                    box.append(rimProduct);
                }
            }
        }

    })
}

//长线游
function getLongSpot() {
    var box = $(".long-box-ul");
    box.html("");
    $.ajax({
        type: "get",
        url: "/product/getlongproduct",
        datatype: "json",
        success: function (jsonResult) {
            console.log(jsonResult);
            var longProductList = jsonResult.data;
            for (var i = 0; i < longProductList.length && i < 8; i++) {
                var previewImg = longProductList[i].previewImg;
                var proId = longProductList[i].id;
                var productName = longProductList[i].productName;
                var productDescription = longProductList[i].productDescription;
                var personPrice = longProductList[i].personPrice;
                var path = location.pathname;
                var webPath = path.substring(0, path.substr(1).indexOf('/') + 1);
                var longProduct = " <input type=\"hidden\" class=\"proId\" value=\"" + proId + "\"> \n" +
                    " <li class=\"long-li\"><a href=\"/product/getOne/" + proId + "\">\n" +
                    "                <div class=\"long-li-img\"><img src=\"" + webPath + previewImg + "\"></div>\n" +
                    "                <div class=\"long-li-message\">\n" +
                    "                    <span class=\"long-item-title\">" + productName + "</span>\n" +
                    "                    <span class=\"long-item-detail\">" + productDescription + "</span>\n" +
                    "                    <span class=\"money-sy\">￥</span><span class=\"long-item-money\">" + personPrice + "</span>\n" +
                    "                </div>\n" +
                    "            </a> </li>";
                box.append(longProduct);
            }
        }
    })
}


//============================跳转搜索页面======================
$(".serch-btn").click(function () {
    var kw = $(".serch-txt").val();
    window.location.href = "/product/search?kw=" + kw;
})

//点击名称进入首页
$(".nav_userName").click(function () {

    $.ajax({
        url: "/order/getId",
        type: "GET",
        datatype: "json",
        success: function (data) {
            console.log(data.data)
            window.sessionStorage.setItem("cusid", data.data)
            $(location).attr('href', 'order');
        }

    })

})
/*======================秒杀==========================*/

setInterval(timer, 1000);
setInterval(getSecKillList, 60 * 60 * 1000)


function timer() {
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth();
    var day = nowDate.getDate();
    var countdown = $(".countdown-box");
    //活动期间 结束倒计时
    if (nowDate.getHours() >= 10 && nowDate.getHours() <= 11) {
        countdown.html("  <div class=\"countdown-desc\">\n" +
            "                    <strong></strong>活动结束倒计时\n" +
            "                </div>\n" +
            "                <div class=\" countdown-main\">\n" +
            "                    <span class=\"time countdown-hours time-1\">00</span><span class=\"time-sy\">&nbsp;:</span>\n" +
            "                    <span class=\"time countdown-minutes\">29</span><span class=\"time-sy\">&nbsp;:</span>\n" +
            "                    <span class=\"time countdown-seconds\">24</span>\n" +
            "                </div>");
        var killDate = new Date(year, month, day, 11, 0, 0, 0);
    }
    //活动未开始  开始倒计时
    if (nowDate.getHours() < 10 || nowDate.getHours() > 11) {
        countdown.html(" <div class=\"countdown-desc\">\n" +
            "                    <strong>10:00</strong>场 倒计时\n" +
            "                </div>\n" +
            "                <div class=\" countdown-main\">\n" +
            "                    <span class=\"time countdown-hours time-1\">00</span><span class=\"time-sy\">&nbsp;:</span>\n" +
            "                    <span class=\"time countdown-minutes\">29</span><span class=\"time-sy\">&nbsp;:</span>\n" +
            "                    <span class=\"time countdown-seconds\">24</span>\n" +
            "                </div>");
        if (nowDate.getHours() > 11) {
            day++;
        }
        var killDate = new Date(year, month, day, 10, 0, 0, 0);
    }

    var nowTime = nowDate.getTime();
    var killTime = killDate.getTime();
    if (nowTime > killTime) {
        var dis = Math.floor((nowTime - killTime) / 1000);

    } else {
        var dis = Math.floor((killTime - nowTime) / 1000);

    }

    //离开始差多少小时
    var lefthors = Math.floor(dis / 3600 % 24);
    //离开始差多少分钟
    var leftMinutse = Math.floor(dis / 60 % 60);
    //离开始差多少秒
    var leftSeconds = Math.floor(dis % 60);


    //若分钟数和秒数小于10 要改成01样式
    if (lefthors < 10) {
        lefthors = "0" + lefthors;
    }
    if (leftMinutse < 10) {
        leftMinutse = "0" + leftMinutse;
    }
    if (leftSeconds < 10) {
        leftSeconds = "0" + leftSeconds;
    }
    //渲染倒计时
    $(".countdown-hours").html(lefthors);
    $(".countdown-minutes").html(leftMinutse);
    $(".countdown-seconds").html(leftSeconds);
}

/*======================渲染秒杀商品列表===========================*/
function getSecKillList() {
    var secKillbox = $(".seckill-box");
    secKillbox.html("");
    $.ajax({
            type: "get",
            url: "/product/getseckill",
            datatype: "json",
            success: function (jsonresult) {
                var dataList = jsonresult.data;
                console.log(dataList);
                var productList = dataList.secProductList;
                var secKillList = dataList.seckillList;
                for (var i = 0; i < productList.length; i++) {
                    var previewImg = productList[i].previewImg;
                    var productName = productList[i].productName;
                    var proId = productList[i].id;
                    // var productDescription = productList[i].productDescription;
                    var personPrice = productList[i].personPrice.toFixed(0);
                    var startCity = productList[i].startCity;
                    var destination = productList[i].destination;

                    var path = location.pathname;

                    for (var j = 0; j < secKillList.length; j++) {
                        if (secKillList[j].productId == proId) {
                            var seckillId = secKillList[j].id;
                            var seckillNumber = secKillList[j].seckillNumber;
                            var seckillRebate = secKillList[j].seckillRebate;
                            var sceKillPrice = (personPrice * seckillRebate).toFixed(0);
                            var webPath = path.substring(0, path.substr(1).indexOf('/') + 1);
                            var seckillLi = " <li class=\"kill-li\">\n" +
                                "                    <a href=\"/product/seckill/" + seckillId + "\" class=\"kill-li-item\">\n" +
                                "                        <div class=\"kill-li-item-top\">\n" +
                                "                            <div class=\"kill-li-item-img\">\n" +
                                "                                <img src=\"" + webPath + previewImg + "\">\n" +
                                "                            </div>\n" +
                                "                            <div class=\"kill-li-item-title\">\n" +
                                "                                <div class=\"right-title-in\">\n" +
                                "                                    <span class=\"right-title-main\">" + startCity + "-" + destination + "</span>\n" +
                                "                                    <span class=\"right-title-sub\">剩余数量：" + seckillNumber + "</span>\n" +
                                "                                </div>\n" +
                                "                            </div>\n" +
                                "                        </div>\n" +
                                "                        <div class=\"kill-li-item-bottom\">\n" +
                                "                            <span class=\"kill-li-item-bottom-title\">" + productName + "</span>\n" +
                                "                            <div class=\"maret-price\"><span>市场价：￥</span><span class=\"maret-money\">" + personPrice + "</span></div>\n" +
                                "                            <div class=\"kill-price\"><span>秒杀价：￥</span><span class=\"kill-money\">" + sceKillPrice + "</span></div>\n" +
                                "                        </div>\n" +
                                "                    </a>\n" +
                                "                </li>"
                            secKillbox.append(seckillLi);
                        }
                    }
                }
            }
        }
    )
}